#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"
#include "string.h"

#ifndef TRUE
    #define  TRUE       ((BOOL)1)
    #define  FALSE      ((BOOL)0)
#endif

#include <errno.h>
#include "msgtool.h"
extern		int NavigationInitFlag;
extern		pid_t NavigationPID;
extern		pid_t UIPID;

#define ITEM_RADIO_LEFT_1	(0)
#define ITEM_RADIO_LEFT_2	(1)
#define ITEM_RADIO_LEFT_3	(2)
#define ITEM_RADIO_RIGHT_1	(3)
#define ITEM_RADIO_RIGHT_2	(4)
#define ITEM_RADIO_RIGHT_3	(5)
#define ITEM_RADIO_SCAN		(6)
#define ITEM_RADIO_AST		(7)
#define ITEM_RADIO_EXIT		(8)


#define ITEM_RADIO_PRESET_QTY	(6)
#define SHORTPRESS 1
#define LONGPRESS 2


#define NAVI_PLAY  1
#define NAVI_PAUSE 2

TRect		MenuRadioTouchRect[IMG_MENU_RADIO_ITEM_QTY];
TPosCoor	MenuRadioItemPos[IMG_MENU_RADIO_ITEM_QTY] = { {47,88}, {47,181}, {47,272}, {217,88}, {217,180}, {217,272}, {9,376}, {151,376}, {577,376}};
TPosCoor 	MenuRadioFreqBkgPos = {406,96};
char*		MenuRadioChannelAST[] = { "SCAN", "AST", "EXIT"};
int		MenuRadioFocus = -1;
TFrame		MenuRadioFrame;
int 		MenuRadioPreFreq [6];
int 		Hilight = 0;
char*		MenuRadioFMChannelFreq[6] = { "100.3", "101.0", "107.1", " 96.0", "108.0", " 97.1" };
char*		MenuRadioAMChannelFreq[6] = { " 533", " 788", "1011", " 968", " 971", "1022" };
char		MenuRadioCurFreq[10] =  {' ','8','7','.','5',' ','M','H','Z','\0'};
char*		MenuRadioFMAM[5] = {"FM1","FM2","FM3","AM1","AM2"};

static int 		FMAM= 0;
static int 		exit_flag = 0;//0:exit is valid,1:exit is invalid.
static int 		scan_flag;
static int 		AST_flag;
static int 		freq_temp;
static int 		disp_class;//1 is display all;2 is display preset;3 is display curfreq;4 is display scan\ast\exit
static int 		disp_count = 0;
static unsigned int scan_disp_count = 0;
static unsigned int scan_delay_count = 0;
static int navi_pause_flag = 0;
int volume_count = 0;
int volume_temp = 0;

//unsigned char radio_data[5] = {0xe0,0x03,0x02,0x02,0xe7};
extern  unsigned char send_data_buf[5];
TRect	CurFreqInfoRect={426,186,646,276}; /*{430,210,650,300};*/
TRect	FMAMInfoRect= {0,8,720,72};
//TPosCoor	CurFreqInfoPos = {430,210};
TPosCoor	FMAMInfoPos = {270,88};
TPosCoor	SereoPos = {589,108};//{580,125};
unsigned char FMAM_TABLE[7] = {0,0,1,2,0,3,4};
static int StereoFlag =0;

extern int 	Last_ID ;
extern unsigned char mediea_flag;
extern unsigned char navi_run;
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
//extern TRect	BarInfoRect;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;	

void RadioDrawMUTE( void);



/*void (*KeyPadDispatch[])() = {RadioDrawMUTE(),MenuRadioDraw( TRUE ),NULL,NULL,\
							MenuNaviInit(),NULL,MenuCdInit(),MenuNaviInit(),MenuMediaInit(),\
							MenuMainInit(),NULL};*/

void (*DisplayScreenDispatch[])() = {NULL,MenuMainInit,NULL,MenuCdInit,MenuCdInit,\
							MenuCdInit,MenuMediaInit,MenuNaviInit,MenuAudioInit,MenuVideoInit,\
							AudioEQInit,AudioBASInit,AudioFADInit,MenuCdInit,MenuCdInit,MenuCdInit};



int 	RadioInfoUpdata (void)
{	
	
	int frq;

	
	frq = read_info_data[4] * 256 + read_info_data[5];	
	
	if (read_info_data[2] == ID_SCREEN_INFO)
		{
			switch (read_info_data[3])
			{
				case 		0x01:
				case 		0x02:				
				case 		0x03:				
				case 		0x04:				
				case 		0x05:
				case 		0x06:
					MenuRadioPreFreq [read_info_data[3]-1] = frq;	
					if (disp_class  == 2)
							{
								disp_count ++;													
								if (disp_count >=6)
									{
										//disp_count = 0;
										disp_class = 0;
										printf ("in RadioInfoUpdata and disp_count >=6\n");
									}
							}
					break;
				
				case 		0x07:
					
					freq_temp = frq;
					if (disp_class == 0)
						disp_class = 3;
					//scan_disp_count ++;								
								
					if (FMAM <3)
						{
							{
	            					int FREQ = frq/10;	            
							MenuRadioCurFreq[0] = ((FREQ / 1000) ? ('0' + (FREQ / 1000)) : ' ');			
							FREQ = FREQ % 1000;	
								
							MenuRadioCurFreq[1] = ((FREQ / 100) ? ('0' + (FREQ / 100)) : '0');
							FREQ = FREQ % 100;
					
							MenuRadioCurFreq[2] = ('0' + (FREQ / 10));
							MenuRadioCurFreq[3] = '.';
							MenuRadioCurFreq[4] = '0'+(FREQ %10);
							MenuRadioCurFreq[5] = ' ';
							MenuRadioCurFreq[6] = 'M';	
							MenuRadioCurFreq[7] = 'H';
							MenuRadioCurFreq[8] = 'z';
							MenuRadioCurFreq[9] = '\0';
							}		
						}
						else
							{						
	            					int FREQ = frq;
							MenuRadioCurFreq[0] = ((FREQ / 1000) ? ('0' + (FREQ / 1000)) : ' ');			
							FREQ = FREQ % 1000;	
								
							MenuRadioCurFreq[1] = ((FREQ / 100) ? ('0' + (FREQ / 100)) : '0');
							FREQ = FREQ % 100;
						
							MenuRadioCurFreq[2] = ('0' + (FREQ / 10));
							MenuRadioCurFreq[3] = '0'+(FREQ %10);
							MenuRadioCurFreq[4] = ' ';
							MenuRadioCurFreq[5] = 'K';
							MenuRadioCurFreq[6] = 'H';
							MenuRadioCurFreq[7] = 'z';
							MenuRadioCurFreq[8] = '\0';
							}
							break;				
			}	
							
		}
		
			
		if (read_info_data[2] == ID_BAND_SEL)
			{	
				disp_class = 2;//////////////////	20080116	
				FMAM = 	FMAM_TABLE[read_info_data[3]];	
			}	
			
	
			if (read_info_data[2] == ID_KEY_STATUS)
			{			
				switch (read_info_data[3])
				{
				case 		0x01:
					switch (read_info_data[4])
					{
						case 		0x01:
							exit_flag = 1;
							break;
						case 		0x02:
							exit_flag = 0;
							break;
						case 		0x05:
							AST_flag = 1;							
							break;
						case 		0x06:
							AST_flag = 0;							
							break;
						case 		0x07:
							scan_flag = 1;							
							break;
						case 		0x08:
							scan_flag = 0;						
							break;
					}						
					break;
				case 		0x05:
					if (read_info_data[4] == 0x02)//stereo on
						StereoFlag =1;				
					else
						StereoFlag =0;
					break;
					
				}				
			}	
		return 1;	
				
}

void MenuRadioDrawButtonInfo( TCanvas *pCanvas)
{
	int x,y;
	TJpeg pJpeg;
	TRect rect;
	char FileName[20];
	int GetJpegIdx;
	int i;
	
	for (i = 0;i < 3; i++)
	{
			x = MenuRadioItemPos[ITEM_RADIO_SCAN +i].x;
			y = MenuRadioItemPos[ITEM_RADIO_SCAN +i].y;
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );	
			/*if ( i==3 )
				GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_EXIT;
			else
				GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_SCAN;
				*/
			GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_EXIT;
 			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);

			pJpeg = GetJpegPtr(FileName);
			rect.left = x;
			rect.right = rect.left + pJpeg.Width;
			rect.top = y;
			rect.bottom = rect.top + pJpeg.Height;			
			g_Puts( pCanvas, FONT33, rect,TEXT_ALIGN_CENTER, MenuRadioChannelAST[i] );
	}
}

void MenuRadioDrawCurFMAMInfo( TCanvas *pCanvas)
{
	//int GetJpegIdx;
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );	
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, FMAMInfoRect,  TEXT_ALIGN_CENTER, MenuRadioFMAM[FMAM] );
	
}
void MenuRadioDrawCurFreqInfo( TCanvas *pCanvas)
{
	int GetJpegIdx;
	char FileName[20];	

	GetJpegIdx = IMG_MENU_RADIO_ITEM_FREQUENCY ;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);
	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );	

	DrawJpeg(pCanvas, CurFreqInfoRect.left, CurFreqInfoRect.top, FileName);		
	g_Puts( pCanvas, FONT33, CurFreqInfoRect, TEXT_ALIGN_CENTER, MenuRadioCurFreq);//MenuRadioFMChannelFreq[FocusIdx] );
	

}

void Int2String(int index)
{
	
	if (FMAM <3)
		{	
			int FREQ = MenuRadioPreFreq [index]/10;
			MenuRadioFMChannelFreq[index][0] = ((FREQ / 1000) ? ('0' + (FREQ / 1000)) : ' ');			
			FREQ = FREQ % 1000;	
								
			MenuRadioFMChannelFreq[index][1] = ((FREQ / 100) ? ('0' + (FREQ / 100)) : '0');
			FREQ = FREQ % 100;
														
			MenuRadioFMChannelFreq[index][2] = ('0' + (FREQ / 10));
			MenuRadioFMChannelFreq[index][3] = '.';
			MenuRadioFMChannelFreq[index][4] = '0'+(FREQ %10);
			MenuRadioFMChannelFreq[index][5] = '\0';		
		}
	else
		{
			int FREQ = MenuRadioPreFreq [index];
			MenuRadioAMChannelFreq[index][0] = ((FREQ / 1000) ? ('0' + (FREQ / 1000)) : ' ');			
			FREQ = FREQ % 1000;	
								
			MenuRadioAMChannelFreq[index][1] = ((FREQ / 100) ? ('0' + (FREQ / 100)) : '0');
			FREQ = FREQ % 100;
							
			MenuRadioAMChannelFreq[index][2] = ('0' + (FREQ / 10));
			MenuRadioAMChannelFreq[index][3] = '0'+(FREQ %10);
			MenuRadioAMChannelFreq[index][4] = '\0';	
			
		}
		
}

void MenuRadioDrawStereo( TCanvas *pCanvas)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];

	if (StereoFlag)	
		GetJpegIdx = IMG_MENU_RADIO_ITEM_STEREO;
	else
		GetJpegIdx = IMG_MENU_RADIO_ITEM_STEREO_COVER;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName );
	rect.left = SereoPos.x;
	rect.right = rect.left + pJpeg.Width;
	rect.top =SereoPos.y ;
	rect.bottom = rect.top + pJpeg.Height;		
	DrawJpeg(pCanvas, SereoPos.x, SereoPos.y, FileName);	
	//printf ("MenuRadioDrawStereo ,GetJpegIdx is %d,left: %d,right:%d,top:%d,bottom:%d\n",GetJpegIdx,rect.left,rect.right,rect.top,rect.bottom);
	dt_Present_part(rect);	
}

void MenuRadioDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{		
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
					GetJpegIdx = IMG_VOLUME_MUTE ;
					sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
					DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
					mute_status = 2;
				
				printf ("VOLvalue is %d \n",VOLvalue);
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				MenuRadioDrawCurFMAMInfo( MenuRadioFrame.pCanvas );
				//dt_Present_part(BarInfoRect);	
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}

static void VolumeCountHandle(void)
{
//static int volume_temp;
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuRadioDrawVolume(MenuRadioFrame.pCanvas,3);	
				//BackToLastID(lastID);	
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void MenuRadioDrawPresInfo( TCanvas *pCanvas, int FocusIdx )
{
	TJpeg pJpeg;
	TRect rect;
	char FileName[20];
	int GetJpegIdx;
	int x,y;

	x = MenuRadioItemPos[FocusIdx].x;
	y = MenuRadioItemPos[FocusIdx].y;
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );	
	GetJpegIdx = IMG_MENU_RADIO_ITEM_FREQ;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	//////DrawJpeg(pCanvas, x, y, FileName);

	rect.left = x;
	rect.right = rect.left + pJpeg.Width;
	rect.top = y;
	rect.bottom = rect.top + pJpeg.Height;
	
	Int2String (FocusIdx);
	if (FMAM <3)
			g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_CENTER, MenuRadioFMChannelFreq[FocusIdx] );
		else
			g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_CENTER, MenuRadioAMChannelFreq[FocusIdx] );	

}


void MenuRadioDrawBackgroud(TCanvas *pCanvas)
{
	char FileName[20];
	//int x_left,y_left;
	//int x_right,y_right;
	int GetJpegIdx;
	

	GetJpegIdx = IMG_MENU_RADIO_BKG_DISP;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	DrawJpeg(pCanvas, MenuRadioFreqBkgPos.x, MenuRadioFreqBkgPos.y,FileName);
}

//can  be changed /////
void MenuRadioDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	char FileName[20]= "";
	int GetJpegIdx;	
	
	if( idx < 0 || idx >= IMG_MENU_RADIO_ITEM_QTY )
		return;
	switch (idx)
		{
			case 		ITEM_RADIO_EXIT:				
				if (exit_flag)//invalid
				{							
					GetJpegIdx = IMG_MENU_RADIO_ITEM_EXIT_VALID ;												
				}
				else				
				{
					if( bfocus)
						GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_EXIT ;
					else
						GetJpegIdx = IMG_MENU_RADIO_ITEM_EXIT;								
				}					
				
				//pos = MenuRadioItemPos[idx];
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);	
			break;
			
			case 		ITEM_RADIO_SCAN:
				if (scan_flag ||bfocus)
				{
					GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_EXIT ;							
				}
				else
				{		
					GetJpegIdx = IMG_MENU_RADIO_ITEM_EXIT;										
				}
				//pos = MenuRadioItemPos[idx];	
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);	
			break;	
			
			case 		ITEM_RADIO_AST:
				if  (AST_flag ||bfocus)				
				{
					GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_EXIT;	
								
				}
				else
				{
					GetJpegIdx = IMG_MENU_RADIO_ITEM_EXIT ;
				}
				//pos = MenuRadioItemPos[idx];	
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);			
	
			break;	
			
			default:
				if( bfocus)					
					GetJpegIdx = IMG_MENU_RADIO_ITEM_FOCUS_FREQ;
				else					
					GetJpegIdx = IMG_MENU_RADIO_ITEM_FREQ;
				//pos = MenuRadioItemPos[idx];
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_FM_FOLDER, GetJpegIdx);
		}
		pJpeg = GetJpegPtr(FileName);
		DrawJpeg(pCanvas, MenuRadioItemPos[idx].x, MenuRadioItemPos[idx].y, FileName);
		MenuRadioTouchRect[idx].left = MenuRadioItemPos[idx].x;
		MenuRadioTouchRect[idx].right = MenuRadioItemPos[idx].x + pJpeg.Width;
		MenuRadioTouchRect[idx].top = MenuRadioItemPos[idx].y;
		MenuRadioTouchRect[idx].bottom = MenuRadioItemPos[idx].y + pJpeg.Height;
	
}


void MenuRadioDraw( BOOL bDrawAll )
{
	int i;
	int temp = 0x00; 
	if( bDrawAll )
		MenuRadioDrawBackgroud(MenuRadioFrame.pCanvas);			
		
	for (i=0; i<6; i++)
	{
			temp = 0x0001<<i;
			if 	(freq_temp == 0)
			{
				for( i = 0; i < ITEM_RADIO_PRESET_QTY; i++ )
				{
					MenuRadioDrawItem( MenuRadioFrame.pCanvas, i, FALSE );
					MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);
				}
				break;		
			}
			if (freq_temp == MenuRadioPreFreq[i]) 
			{																			
				MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  TRUE );														
				MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);	
				Hilight |= temp;								
			}
			else
			{																						
				MenuRadioDrawItem( MenuRadioFrame.pCanvas, i, FALSE );
				MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);
				Hilight &= ~temp;										
			}
		}

	for (i= ITEM_RADIO_SCAN; i<= ITEM_RADIO_EXIT;i++)
		MenuRadioDrawItem( MenuRadioFrame.pCanvas, i, FALSE );
	MenuRadioDrawButtonInfo( MenuRadioFrame.pCanvas);	
	
	 MenuRadioDrawCurFMAMInfo( MenuRadioFrame.pCanvas );
	 MenuRadioDrawCurFreqInfo ( MenuRadioFrame.pCanvas );
	 MenuRadioDrawStereo(MenuRadioFrame.pCanvas );
	 
	if (mute_status == 1)
	{
		MenuRadioDrawVolume(MenuRadioFrame.pCanvas,1);	
	}
	//dtpresent_flag++;		
	//pthread_cond_signal(&dt_cond);	
	dt_Present();
}

void Cur_freq_handle(void)
{
	int i,temp;
	if (disp_class == 3) 
	{	
		//dt_Present_part(CurFreqInfoPos);	///////////////20080201			
		if (!scan_flag )
		{					
			for (i=0; i<6; i++)
			{
				temp = 0x0001<<i;																
				if (freq_temp == MenuRadioPreFreq[i]) 
				{		
					if (!(Hilight & temp))	
					{										
						MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  TRUE );	
						MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);	
						dt_Present_part(MenuRadioTouchRect [i]);	
						Hilight |= temp;										
					//	printf ("in cc \n");	
						}
					}
					else
					{
						if ( (freq_temp != MenuRadioPreFreq[i]) &&(Hilight & temp) ) 
						{											
							MenuRadioDrawItem( MenuRadioFrame.pCanvas, i, FALSE );
							MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);
							dt_Present_part(MenuRadioTouchRect [i]);	
							Hilight &= ~temp;		
						//	printf ("in dd temp is %d\n",temp);									
						}
					}	
					//printf ("temp: %d,Hilight: 0x%x \n",temp,Hilight);																		
				}
			}
			//	printf ("in Cur_freq_handle \n");					
			MenuRadioDrawCurFMAMInfo( MenuRadioFrame.pCanvas );	
			MenuRadioDrawCurFreqInfo( MenuRadioFrame.pCanvas );	
			if ( ir_GetCurrentID() == IR_MENU_RADIO_INIT)
			{
				dt_Present_part(CurFreqInfoRect);	
				dt_Present_part(FMAMInfoRect);	
				//printf ("in scan test wether dt_present ~~\n");
			}
			else
			{
				dtpresent_flag++;		///////////20080221 can be changed 
				pthread_cond_signal(&dt_cond);
			}
			disp_class = 0;
		}
}

int MenuRadioGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MENU_RADIO_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MenuRadioTouchRect[i] ) )
			return i;
	return -1;
}

BOOL MenuRadioBtnEnter(int idx)
{
	int temp;
	switch(idx)
	{
		case ITEM_RADIO_LEFT_1:
		case ITEM_RADIO_LEFT_2:
		case ITEM_RADIO_LEFT_3:
		case ITEM_RADIO_RIGHT_1:
		case ITEM_RADIO_RIGHT_2:
		case ITEM_RADIO_RIGHT_3:
			
			MenuRadioDrawItem( MenuRadioFrame.pCanvas, idx, FALSE );
			MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, idx);
			temp = 0x0001<<idx;	
			Hilight &= ~temp;	
			dt_Present_part(MenuRadioTouchRect [idx]);					
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);					
			//dt_Present();
			break;
			
		case ITEM_RADIO_SCAN:
			MenuRadioDrawItem( MenuRadioFrame.pCanvas, idx, FALSE );
			 MenuRadioDrawButtonInfo( MenuRadioFrame.pCanvas);
			 dt_Present_part(MenuRadioTouchRect [idx]);	
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);			 			
			//dt_Present();
			break;
			
		case ITEM_RADIO_AST:
			MenuRadioDrawItem( MenuRadioFrame.pCanvas, idx, FALSE );
			 MenuRadioDrawButtonInfo( MenuRadioFrame.pCanvas);
			 dt_Present_part(MenuRadioTouchRect [idx]);		
			 //dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);			
			//dt_Present();
			break;

		case ITEM_RADIO_EXIT:
			MenuMainInit();						 
			break;
	}
	LOG( "btn = %d\n", idx );	
	return TRUE;
}



void MenuRadioInit()
{	
	ir_SetCurrentID( IR_MENU_RADIO_INIT );
	scan_flag =0;//////////////1219 add 
	AST_flag = 0;
	//disp_class = 0;	
	Hilight = 0;
	mediea_flag = 0;	
	frm_Create( &MenuRadioFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	MenuRadioDraw( TRUE );
	printf ("in radio init !!!\n");
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	int i,temp;
	switch (read_info_data[2])
	{
		case	ID_POWER:						
			break;						
		case	ID_DISPLAY_SCREEN:
			if (read_info_data[3] ==ID_SCREEN_MEDIEA)
			{
				switch (Last_ID)
				{
					case  IR_MEDIA_PHOTO_INIT:
						MediaPhotoInit();
						break;
					case IR_MEDIA_MUSIC_INIT:
						MediaMusicInit();
						break;
					case IR_MEDIA_MOVIE_INIT:
						MediaMovieInit();
						break;
						default:
						MenuMediaInit();													
				}	
			}
			else
				if (DisplayScreenDispatch[read_info_data[3]]!=NULL)
					DisplayScreenDispatch[read_info_data[3] ] (NULL);						
				rc = 0;//if handled, clear this flag.	
				read_info_data[0] = 0;																									
				break;
						
		case	ID_KEY_STATUS:	
			switch (read_info_data[3])
			{
				case  0x01:											
				{
					if (scan_flag && AST_flag)
					{
						scan_flag = 0;
						AST_flag = 0;
					}
			/////////////// update the preset button to false 080110 /////////////
					if (scan_flag ||AST_flag ) 
					{	
						int temp = 0x0001;
						for( i = 0; i < ITEM_RADIO_PRESET_QTY; i++ )
						{
							if (Hilight &(temp<<i))
							{
								MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  FALSE );
								MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);
								Hilight &= ~temp;
								dt_Present_part(MenuRadioTouchRect[i]);
							}
						}			
					}	
					else
					{
						int i,temp;
						for( i = 0; i < ITEM_RADIO_PRESET_QTY; i++ )
						{
							temp = 0x0001<<i;	
							if (Hilight & temp)
							{
								MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  TRUE );
								Hilight |= temp;
								MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);																	
								dt_Present_part(MenuRadioTouchRect[i]);
								//printf ("in true 11__\n");
							}
							else
							{
								MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  FALSE );
								Hilight &= ~temp;
								MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);																	
								dt_Present_part(MenuRadioTouchRect[i]);
								//printf ("in faulse 22__\n");
							}
								//MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);																	
								//dt_Present_part(MenuRadioTouchRect[i]);

						}
					}
					
													
					for (i= ITEM_RADIO_SCAN; i<= ITEM_RADIO_EXIT;i++)
					{
						MenuRadioDrawItem( MenuRadioFrame.pCanvas, i, FALSE );
						MenuRadioDrawButtonInfo( MenuRadioFrame.pCanvas);
						dt_Present_part(MenuRadioTouchRect[i]);
					}
												
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);	
				}
			break;
			
				case	0x05://stereo logo
					MenuRadioDrawStereo(MenuRadioFrame.pCanvas );
					break;
				}
						
			rc = 0;//if handled, clear this flag.	
			read_info_data[0] = 0;						
			break;
						
		case	ID_SCREEN_INFO:			
			if (disp_class == 3) 
			{
			//printf ("in freq info 02:scan_disp_count = %d\n",scan_disp_count);
				if (scan_disp_count == 0)
				{																				
					if (navi_run && (navi_pause_flag != NAVI_PAUSE ) )	
					{	
						Message qbuf;					
						qbuf.mtype = NavigationPID;
						qbuf.mcommond = MQ_COMMOND_PAUSE_ACTIVE;
						sprintf(qbuf.mtext, "MQ_COMMOND_PAUSE_ACTIVE");/////////////20080414
						send_message(&qbuf, 0);	
						printf ("in send message MQ_COMMOND_PAUSE_ACTIVE \n");
						navi_pause_flag = NAVI_PAUSE;																								
					}		
					else 
						if (!navi_run)	
						{	
							navi_pause_flag = 0;												 		
						}
					scan_disp_count ++;	
					scan_delay_count ++;				
					//printf ("in freq info 03:scan_disp_count is %d\n",scan_disp_count);
				}
					else
						{
							scan_disp_count ++;	
							scan_delay_count ++;	
							if (scan_disp_count >1)///////can be change for speed 20080717
							{											
								Cur_freq_handle();
								//printf ("in scan_disp_count >10\n");
								scan_disp_count = 0;
								scan_delay_count =1;
							}		
						}							
				}
				if (read_info_data[3]<7)
					{					
					int temp,i;	
					i = read_info_data[3]-1;
					temp = 0x0001<<i;	
					//printf ("in freq info 04:disp_count is %d,disp_class is %d,i is %d\n",disp_count,disp_class,i);	
				if (freq_temp == MenuRadioPreFreq[i]) 
				{		
						MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  TRUE );
								Hilight |= temp;
								MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);																	
								dt_Present_part(MenuRadioTouchRect[i]);
								//printf ("in true 33___\n");
					}
					else
					{
						MenuRadioDrawItem( MenuRadioFrame.pCanvas, i,  FALSE );
								Hilight &= ~temp;
								MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, i);																	
								dt_Present_part(MenuRadioTouchRect[i]);
								//printf ("in faulse 44___\n");
					}	
					
					}
						
													
				if (read_info_data[3] == ID_SCREEN_INFO_VOLUME )
				{
					MenuRadioDrawVolume(MenuRadioFrame.pCanvas,1);
					break;
				}						
							
			break;
						
		case	ID_KEYPAD:	
			switch (read_info_data[3])
			{
				case 	ID_MUTE:
					MenuRadioDrawVolume(MenuRadioFrame.pCanvas,1);
					break;
				case	ID_DMUTE:
					//MenuRadioDraw( TRUE );
					MenuRadioDrawVolume(MenuRadioFrame.pCanvas,3);	
					break;									
				case 	ID_SEEKUP:
				case 	ID_SEEKDN:
					break;
				case 	ID_DEST:
				case 	ID_NAVI:
					MenuNaviInit();
					break;
				case 	ID_FMAM:
					//band command;
					break;
				case 	ID_CD:
					MenuCdInit();
					break;				
				case 	ID_MEDIA:
					MenuMediaInit();
					break;
				case 	ID_MENU:
					MenuMainInit();
					break;
				case	ID_TVAUX:
					break;
		}	
		rc = 0;//if handled, clear this flag.	
		break;
						
	case 	ID_BAND_SEL:
		rc = 0;//if handled, clear this flag.	
		read_info_data[0] = 0;			
		break;		
	case 	ID_RADIO_PRSETSLE:
		read_info_data[0] = 0;
		break;				
	default:	
		rc = -1;						
	}
	return rc;
	return 0;					
}
void FreqAutoAdd (void)
{
	int FRQ;
	if (FMAM <3)
	{
	
		if (freq_temp >=10800)
			freq_temp = 8750;
		else
	            	freq_temp += 10 ;
	            	 FRQ = freq_temp/10;
	            //printf (" FreqAutoAdd freq: %d \n",freq_temp);
			MenuRadioCurFreq[0] = ((FRQ / 1000) ? ('0' + (FRQ / 1000)) : ' ');			
			FRQ = FRQ % 1000;	
								
			MenuRadioCurFreq[1] = ((FRQ / 100) ? ('0' + (FRQ / 100)) : '0');
			FRQ = FRQ % 100;
					
			MenuRadioCurFreq[2] = ('0' + (FRQ / 10));
			MenuRadioCurFreq[3] = '.';
			MenuRadioCurFreq[4] = '0'+(FRQ %10);
			MenuRadioCurFreq[5] = '\0';									
		}		
						
		else
		{	
			if (freq_temp >=1620)
				freq_temp = 522;
			else		
	            		freq_temp += 9 ;
	            FRQ = freq_temp;
			MenuRadioCurFreq[0] = ((FRQ / 1000) ? ('0' + (FRQ / 1000)) : ' ');			
			FRQ = FRQ % 1000;	
								
			MenuRadioCurFreq[1] = ((FRQ / 100) ? ('0' + (FRQ / 100)) : '0');
			FRQ = FRQ % 100;
					
			MenuRadioCurFreq[2] = ('0' + (FRQ / 10));
			MenuRadioCurFreq[3] = '0'+(FRQ %10);
			MenuRadioCurFreq[4] = '\0';	
		}	
}
/*
static int send_data_out ( int index,int short_long)
{
	int offset = 0;
	if ((index <0) || ( index > IMG_MENU_RADIO_ITEM_QTY))
		return -1;
		
		if (index <= ITEM_RADIO_EXIT )
			offset = index;
		
		if (short_long == SHORTPRESS)
			{
				radio_data [3] =  0x02 + offset;
				radio_data [4] =(unsigned char )(0xe7 + offset);
			}
		if (short_long == LONGPRESS)
			{
				radio_data [3] =  0x0b + offset;
				radio_data [4] =(unsigned char )(0xf0 + offset);
			}
			
		if (dev_SPIWrite(radio_data,5)==0)
			return 0;//hangsheng 071126
	return -1;
		
}

*/
static int send_data ( int index,int short_long_press)
{
	int offset = 0;
	//unsigned char BT_tesdata1[16] = {0xe0,0x0e,0x09,0x30,0x37,0x35,0x35,0x32,0x36,0x37,0x31,0x39,0x39,0x32,0x36,0x72};
	//unsigned char BT_tesdata2[11] = {0xe0,0x09,0x09,0x31,0x30,0x31,0x33,0x30,0x38,0x39,0x58};
	
	if ((index <0) || ( index > IMG_MENU_RADIO_ITEM_QTY))
		return -1;
		
		if (index <= ITEM_RADIO_EXIT )
			offset = index;
		
		if (short_long_press == SHORTPRESS)
			{
				send_data_buf [3] =  RADIO_PRE1+ offset;				
			}
		if (short_long_press == LONGPRESS)
			{
				send_data_buf [3] =  RADIO_PR1_L+ offset;				
			}		
			
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		send_data_buf [2] = IDDATA_RADIO;	
		#if 0 ////////////test 20081126 ///////////
		if (index == ITEM_RADIO_SCAN)
			return ( send_data_out(BT_tesdata1,BT_tesdata1[1]+2) );
		else
			if (index == ITEM_RADIO_AST)
			return ( send_data_out(BT_tesdata2,BT_tesdata2[1]+2) );
			printf ("in send need data 1126\n");
			
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		#else			
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		#endif
}



extern pthread_cond_t dt_cond;
void MenuRadioInput(UInt32 key)
{
	int flag;
	static int last_focus;
	static int count=0;	
		static int scan_count = 0;
	static int press = SHORTPRESS;
	static unsigned char last_press;//1 is long press
	TPosCoor pos;

	if ( scan_delay_count>0 )
		{
			scan_delay_count ++;	
			//printf ("		scan_delay_count is %d \n",scan_delay_count);	
			if (scan_delay_count > 10000)
			{					
				Cur_freq_handle();	
				//printf ("		scan_delay_count >10000 \n");					
				scan_count = 1;//scan_count ++;
				if (!navi_run)	
				{									
					navi_pause_flag = 0;
					scan_count = 0;	
				}	
				scan_disp_count = 0;	
				scan_delay_count =0;
			}
		}
		
			
	if (scan_count>0)
		scan_count ++;
	if ((scan_count > 5000) && navi_run && (navi_pause_flag != NAVI_PLAY))
	{		
		Message qbuf;				
		qbuf.mtype = NavigationPID;
		qbuf.mcommond = MQ_COMMOND_PLAY_ACTIVE;
		sprintf(qbuf.mtext, "MQ_COMMOND_PLAY_ACTIVE");/////////////20080414
		send_message(&qbuf, 0);									
		navi_pause_flag = NAVI_PLAY;	
		scan_count = 0;																							
	}		
			
			
	if ( key == IRC_READ_INFO)
	{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main radio handle receive data error!\n");	
			if (flag == 0)
			{					
					return;
			}			
	}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;		
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
				{
					if( ( MenuRadioFocus = MenuRadioGetTouchID( pos )) != -1 )
					{							
						MenuRadioDrawItem( MenuRadioFrame.pCanvas, MenuRadioFocus, TRUE );
						if (MenuRadioFocus <IMG_MENU_RADIO_ITEM_QTY -3)
						{
							MenuRadioDrawPresInfo(MenuRadioFrame.pCanvas, MenuRadioFocus);												
						}		
						else
						MenuRadioDrawButtonInfo( MenuRadioFrame.pCanvas);	
						dt_Present_part(MenuRadioTouchRect [MenuRadioFocus]);	
						//if ( send_data_out(MenuRadioFocus))
						//	printf ("write failure !\n");		
						count = 0;			
					}
				
				}
			else
				{
					if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP ||key == IRC_TOUCH_REPEAT||key == IRC_MOUSE_MOVE)
					{
						count ++;
						if ((count >10) &&(MenuRadioFocus <IMG_MENU_RADIO_ITEM_QTY -3))
						{									
							count = 0;
							press = LONGPRESS;
							if ( send_data(MenuRadioFocus,press))
							printf ("write failure1 !\n");		
							last_press = 1;
																		
						}								
					}
					
				}
		}
		else
		{
			if(( ( focus = MenuRadioGetTouchID( pos ) ) == MenuRadioFocus ) ||(MenuRadioFocus != -1 ))
				{
					#ifdef INVALID_KEY
					if ((MenuRadioFocus != ITEM_RADIO_EXIT) || ((MenuRadioFocus == ITEM_RADIO_EXIT)&&(!exit_flag))) //exit valid
					
					#endif
					{
						if ( !last_press)
							MenuRadioBtnEnter( MenuRadioFocus );//when release ,call button function.
						else
							last_press = 0;								
					}
				
				if (press == SHORTPRESS)
				{
					//dt_Present();//20080115
					if ( send_data(MenuRadioFocus,press))
						printf ("write failure2 !\n");	
				}
				else
				{	
					press = SHORTPRESS;
				}
			}
				
			MenuRadioFocus = -1;	
			count = 0;
		}
		LastKey = key;//hangsheng add 071126	
		last_focus = 	MenuRadioGetTouchID( pos );		
	}
	if(mute_status ==2) //is not mute
	VolumeCountHandle ();
	
}

